<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>

    <link rel="stylesheet" href="../lib/swiper-bundle.min.css" />
    <script src="../lib/vue2.js"></script>

    <style>
      #box {
        width: 50%;
        height: 300px;
        background-color: antiquewhite;
        margin: 80px auto;
      }
      .mySwiper {
        width: 100%;
        height: 100%;
      }
    </style>
  </head>
  <body>
    <div id="box">
      <div class="swiper mySwiper">
        <div class="swiper-wrapper">
          <!-- 轮播内容 -->
          <div class="swiper-slide" v-for="item in swiperList" :key="item">
            {{item}}
          </div>
        </div>

        <!-- 分页器 -->
        <div class="swiper-pagination"></div>
      </div>
    </div>
    <script src="../lib/swiper-bundle.min.js"></script>
    <script>
      new Vue({
        el: "#box",
        data() {
          return {
            swiperList: [],
          };
        },

        // dom挂载
        mounted() {
          // 模拟ajax异步获得数据
          setTimeout(() => {
            this.swiperList = ["1", "2", "3"];
          }, 2000);
        },
        //更新后初始化
        // 存在问题，每次页面发生变化都会初始化，出现多个swiper对象
        updated() {
          init();
        },
      });

      function init() {
        new Swiper(".mySwiper", {
          //     navigation: {
          //   nextEl: ".swiper-button-next",
          //   prevEl: ".swiper-button-prev",
          // },
          pagination: {
            el: ".swiper-pagination",
          },
        });
      }
    </script>
  </body>
</html>
